<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2084 – Remove `$` or use `_=$((expr))` to avoid executing output.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2084 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2084">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="remove--or-use-_expr-to-avoid-executing-output">Remove
<code>$</code> or use <code>_=$((expr))</code> to avoid executing
output.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2084.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="va">i</span><span class="op">=</span>4</span>
<span id="cb1-2"><a href="SC2084.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="va">$((</span> <span class="va">i</span><span class="op">++</span> <span class="va">))</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<p>Bash, Ksh:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2084.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">i</span><span class="op">=</span>4</span>
<span id="cb2-2"><a href="SC2084.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="kw">((</span> <span class="va">i</span><span class="op">++</span> <span class="kw">))</span></span></code></pre></div>
<p>POSIX (assuming <code>++</code> is supported):</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2084.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="va">i</span><span class="op">=</span>4</span>
<span id="cb3-2"><a href="SC2084.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="va">_</span><span class="op">=</span><span class="va">$((</span> <span class="va">i</span><span class="op">++</span> <span class="va">))</span></span></code></pre></div>
<p>Alternative POSIX version that does not preserve the exit code:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2084.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="bu">:</span> <span class="va">$((</span> <span class="va">i</span><span class="op">++</span> <span class="va">))</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>$((..))</code> expands to a number. If it's the only word on
the line, the shell will try to execute this number as a command
name:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="SC2084.html#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> i=4</span>
<span id="cb5-2"><a href="SC2084.html#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> <span class="va">$((</span> <span class="va">i</span><span class="op">++</span> <span class="va">))</span></span>
<span id="cb5-3"><a href="SC2084.html#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="ex">4:</span> command not found</span>
<span id="cb5-4"><a href="SC2084.html#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> echo <span class="va">$i</span></span>
<span id="cb5-5"><a href="SC2084.html#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="ex">5</span></span></code></pre></div>
<p>To avoid trying to execute the number as a command name, use one of
the methods mentioned:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb6-1"><a href="SC2084.html#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> i=4</span>
<span id="cb6-2"><a href="SC2084.html#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> _=<span class="va">$((</span> <span class="va">i</span><span class="op">++</span> <span class="va">))</span></span>
<span id="cb6-3"><a href="SC2084.html#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> echo <span class="va">$i</span></span>
<span id="cb6-4"><a href="SC2084.html#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="ex">5</span></span></code></pre></div>
<h3 id="exceptions">Exceptions:</h3>
<p>None.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


